package com.shujia.moniqiefeng.mrsx.put;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

//总共有一千条数据，假设一条数据就是1M，那么按照假设来说每128条数据就是一个block
public class Putdata {
    public static void main(String[] args) throws Exception{
        //创建字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("hodoop//data//students.txt"));
        //创建索引表示是第几个block模块
        int index = 0;
        //创建字符缓冲输出流
        BufferedWriter bw= new BufferedWriter(new FileWriter("hodoop//out/block//block" + index));
        //创建一个集合对象保存读取到的数据
        ArrayList<String> list = new ArrayList<>();
        int rowNumber = 0;
        String line = null;

        while ((line = br.readLine())!=null){
            rowNumber++;
            list.add(line);
            //触发条件来产生block
            if (rowNumber == 140) {
                //触发block写的操作
                for (int i = 128*index; i < 128*index+128; i++) {
                    //可以使用索引获取list的元素
                    String info = list.get(i);
                    //写到一个block模块中
                    bw.write(info);
                    bw.newLine();
                    bw.flush();
                }
                rowNumber=12;
                index++;
                bw = new BufferedWriter(new FileWriter("hodoop//out/block//block" + index));
            }

        }
        for (int i = list.size()-rowNumber; i < list.size(); i++) {
            String info = list.get(i);
            bw.write(info);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();


    }
}
